﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Reticulum
{
    public partial class SealForm : Form
    {
        public SealForm()
        {
            InitializeComponent();
        }

        private void SealForm_Load(object sender, EventArgs e)
        {
            string sql = "select UserName,UserAccount,Seal from Users ";
            dataGridView1.AutoGenerateColumns = false;
            dataGridView1.DataSource = DBHelps.Adapter(sql).Tables[0];
        }
        int index = 0;

        private void dataGridView1_CellMouseUp(object sender, DataGridViewCellMouseEventArgs e)
        {
            if (e.Button == System.Windows.Forms.MouseButtons.Right)
            {
                this.dataGridView1.Rows[e.RowIndex].Selected = true;//是否选中当前行
                index = e.RowIndex;
                this.dataGridView1.CurrentCell = this.dataGridView1.Rows[e.RowIndex].Cells[0];

                //每次选中行都刷新到datagridview中的活动单元格
                this.contextMenuStrip1.Show(this.dataGridView1, e.Location);
                //指定控件（DataGridView），指定位置（鼠标指定位置）
                this.contextMenuStrip1.Show(Cursor.Position);//锁定右键列表出现的位置
            }
        }

        private void 封号ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string account = dataGridView1.Rows[index].Cells["UserAccount"].Value.ToString();//获取选中行的账号
            string name = dataGridView1.Rows[index].Cells["UserName"/*这是dataGridView列里的名*/].Value.ToString();//获取用户名
            if (!this.dataGridView1.Rows[index].IsNewRow)//判断是否为新行
            {
                DialogResult Dr = MessageBox.Show("是否确定封禁：" + name + "这位用户信息？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (Dr == DialogResult.OK)
                {
                    string sql = string.Format("update Users set Seal = 1 where UserAccount='{0}'", account);
                    bool result = DBHelps.Command(sql);
                    if (result)
                    {
                        MessageBox.Show(name + "该用户已被封禁了！");
                        SealForm_Load(sender, e);
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择有效数据！");
            }
        }

        private void 解放ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            string account = dataGridView1.Rows[index].Cells["UserAccount"].Value.ToString();//获取选中行的账号
            string name = dataGridView1.Rows[index].Cells["UserName"/*这是dataGridView列里的名*/].Value.ToString();//获取用户名
            if (!this.dataGridView1.Rows[index].IsNewRow)//判断是否为新行
            {
                DialogResult Dr = MessageBox.Show("是否确定解封：" + name + "这位用户信息？", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Question);
                if (Dr == DialogResult.OK)
                {
                    string sql = string.Format("update Users set Seal = 0 where UserAccount='{0}'", account);
                    bool result = DBHelps.Command(sql);
                    if (result)
                    {
                        MessageBox.Show(name + "该用户已解封了！");
                        SealForm_Load(sender, e);
                    }
                }
            }
            else
            {
                MessageBox.Show("请选择有效数据！");
            }
        }

        private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SealForm_Load(sender, e);
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string sql = "update Users set ThisIP=null";
            DBHelps.Command(sql);
        }
    }
}
